﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MessMain
{
    public partial class Comunication : Form
    {
        public Client cl;
        public static string myName { get; set; }

        private string serverIP;
        private int serverPort;
        private string limba;

        public static string FTPIp;
        public static int FTPPort;
        public static string FTPUser;
        public static string FTPPass;
        
        public Comunication(string limba,
                            string serverIP,
                            string serverPort,
                            string FTPIp,
                            string FTPPort,
                            string FTPUser,
                            string FTPPass,
                            string name)
        {
            InitializeComponent();
            Rename(limba);

            this.serverIP = serverIP;
            this.serverPort = Int32.Parse(serverPort);
            this.limba = limba;
            
            // init static fields
            Comunication.FTPIp = FTPIp;
            Comunication.FTPPort = Int32.Parse(FTPPort);
            Comunication.FTPUser = FTPUser;
            Comunication.FTPPass = FTPPass;
            Comunication.myName = name;

            tablist = new List<TabItems>();

            run();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            System.Environment.Exit(0);
            // Cancel the close...
            e.Cancel = true;
        }

        private bool tabCtl2(string g)
        {
            for (int i = 0; i < this.tabControl2.Controls.Count; i++)
            {
                if (this.tabControl2.Controls[i].Text.Equals(g))
                {
                    return true;
                }
            }
            return false;
        }

        private TabItems getTabList(string g)
        {
            TabItems us = null;
            for (int i = 0; i < tablist.Count; i++)
            {
                if (tablist[i].Text.Equals(g))
                {
                    us = tablist[i];
                }
            }
            return us;
        }

        private void users_DoubleClick(object sender, EventArgs e)
        {
            if (this.users.SelectedIndex != -1)
            {
                if (!tabCtl2((string)this.users.SelectedItem))
                {
                    TabItems itm;
                    if ((itm = getTabList((string)this.users.SelectedItem)) != null)
                    {
                        setCtlTab2(itm);
                    }
                    else
                    {
                        addUserInTabCtl((string)users.SelectedItem);
                    }
                }                
            }
        }

        public void addUserInTabCtl(string name)
        {
            tbi = new TabItems(limba);
            tbi.c = this;
            tbi.myName = Comunication.myName;
            tbi.addUser(name);
            tbi.upDateVar();
            tablist.Add(tbi);
            setCtlTab2(tbi);
        }
        public void setCtlTab2(TabItems tbi)
        {
            this.tabControl2.Controls.Add(tbi); 
        }      

       
        private void users_SelectedIndexChanged(object sender, EventArgs e)
        {
            //this.text1.Text += this.users.SelectedIndex;
            //this.text1.Text += "\r\n";
        }

        public void run()
        {
            cl = new Client(serverIP, serverPort, this, limba);
            cl.init();

            Thread th = new Thread(new ThreadStart(cl.run));
            th.Start();

            UpDateList up = new UpDateList(cl);
            th = new Thread(new ThreadStart(up.run));
            th.Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (frm == null)
            {
                frm = new CreareConf(this, limba);
                frm.Show();  
            }
           frm.setVisible(true);          
        }

        private void iesireC_Click(object sender, EventArgs e)
        {

        }

    }
}
